<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="奖惩类型" prop="rewardsType">
            <el-select v-model="queryParams.rewardsType" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in rewardsTypeOptions" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="员工姓名" prop="staffName">
            <el-input v-model="queryParams.staffName" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:CldRewards:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAdd">新增奖惩
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CldRewardsList" border>
      <el-table-column align="center" label="项目区域" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目名称" prop="projectName"/>
      <el-table-column align="center" label="岗位" prop="projectPostName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_post" :value="scope.row.projectPostName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="姓名" prop="staffName"/>
      <el-table-column align="center" label="职务" prop="jobName"/>
      <el-table-column align="center" label="类型" prop="rewardsType">
        <template slot-scope="scope">
          <dict-tag :options="rewardsTypeOptions" :value="parseInt(scope.row.rewardsType)"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="奖罚金额" prop="rewardsMoney">
        <template slot-scope="scope">
          {{ toMoney(scope.row.rewardsMoney) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="说明" prop="rewardsRemark" width="500px;"/>
      <el-table-column align="center" label="状态" prop="approveStatus">
        <template slot-scope="scope">
          <dict-tag :options="approveStatusOptions" :value="parseInt(scope.row.approveStatus)"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作人" prop="createName"/>
      <el-table-column align="center" label="申请时间" prop="createTime"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.approveStatus == 2" v-hasPermi="['cld:CldRewards:edit']" size="mini" type="text"
                     @click="handleUpdate(scope.row, 1)">修改
          </el-button>
          <el-button v-if="scope.row.approveStatus == 1" size="mini" type="text" v-hasPermi="['cld:CldRewards:audit']"
                     @click="handleUpdate(scope.row, 2)">审核
          </el-button>
          <el-button v-if="scope.row.approveStatus == 1" v-hasPermi="['cld:CldRewards:remove']" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改奖惩管理对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body title="新增奖惩" width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-row>
          <el-col :span="8">
            <el-form-item label="项目名称" prop="projectId">
              <el-select v-model="form.projectId" placeholder="请选择" @change="handleProjectChange">
                <el-option v-for="item in projectOptions" :key="item.projectId" :label="item.projectName"
                           :value="item.projectId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="奖惩类型" prop="rewardsType">
              <el-select v-model="form.rewardsType" placeholder="请选择" @change="handleProjectChange">
                <el-option v-for="item in rewardsTypeOptions" :key="item.value" :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="奖惩金额" prop="rewardsMoney">
              <el-input v-model="form.rewardsMoney" placeholder="请输入" type="number">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="奖惩说明" prop="rewardsRemark">
              <el-input v-model="form.rewardsRemark" :rows="2" placeholder="请输入" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button :disabled="!form.projectId" icon="el-icon-plus" plain size="mini" type="primary"
                       @click="handleAddStaff">添加员工
            </el-button>
          </el-col>
        </el-row>
        <el-table :data="form.staffList" border>
          <el-table-column align="center" label="姓名" prop="staffName"/>
          <el-table-column align="center" label="职务" prop="projectDuitsName">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.cld_project_duits" :value="scope.row.projectDuitsName"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="性别" prop="staffSex"/>
          <el-table-column align="center" label="年龄">
            <template slot-scope="scope">
              {{ calculateAge(scope.row.staffIdcard) }}
            </template>
          </el-table-column>
          <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleDeleteStaff(scope.row)">移除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改或者审核 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="auditOpen" append-to-body title="审核" width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-row>
          <el-col :span="8">
            <el-form-item label="项目名称" prop="projectId">
              <el-select v-model="form.projectId" :disabled="true" placeholder="请选择" @change="handleProjectChange">
                <el-option v-for="item in projectOptions" :key="item.projectId" :label="item.projectName"
                           :value="item.projectId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="奖惩类型" prop="rewardsType">
              <el-select v-model="form.rewardsType" placeholder="请选择" @change="handleProjectChange">
                <el-option v-for="item in rewardsTypeOptions" :key="item.value" :label="item.label"
                           :value="parseInt(item.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="奖惩金额" prop="rewardsMoney">
              <el-input v-model="form.rewardsMoney" placeholder="请输入" type="number">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="奖惩说明" prop="rewardsRemark">
              <el-input v-model="form.rewardsRemark" :rows="2" placeholder="请输入" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-descriptions :column="4" border>
          <el-descriptions-item label="姓名">{{ form.staffName }}</el-descriptions-item>
          <el-descriptions-item label="职务">
            <dict-tag :options="dict.type.cld_project_duits"
                      :value="form.projectDuitsName"/>
          </el-descriptions-item>
          <el-descriptions-item label="性别">{{ form.staffSex }}</el-descriptions-item>
          <el-descriptions-item label="年龄">{{ calculateAge(form.staffIdcard) }}</el-descriptions-item>
        </el-descriptions>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="type == 1" type="primary" @click="submitForm">保存</el-button>
        <el-button v-if="type == 2" type="primary" @click="handleAuditOptions(3)">通过</el-button>
        <el-button v-if="type == 2" type="primary" @click="handleAuditOptions(2)">拒绝</el-button>
        <el-button @click="auditOpen = false">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 选择人员 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="isStaffOpen" append-to-body title="选择人员"
               width="70%">
      <SelectedStaff v-if="isStaffOpen" :isMultiple="true" :isProject="false" :params="{ notStaffIds: notStaffIds.join(','), staffStatus: 2, projectId: form.projectId }"
                     @multipleInput="handleStaffSelected">
      </SelectedStaff>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSelectedStaff">确 定</el-button>
        <el-button @click="isStaffOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCldRewards,
  auditCldRewards,
  delCldRewards,
  getCldRewards,
  listCldRewards,
  updateCldRewards
} from "@/api/cld/CldRewards";
import {getList} from '@/api/cld/project'
import {approveStatusOptions, rewardsTypeOptions} from '@/assets/staticDict'
import SelectedStaff from '@/views/cld/staff/selectedStaff';

export default {
  name: "CldRewards",
  components: {SelectedStaff},
  dicts: ['cld_project_post', 'cld_project_region', 'cld_project_duits'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 奖惩管理表格数据
      CldRewardsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核弹框
      auditOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectRegion: null,
        projectName: null,
        rewardsType: null,
        staffName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          {required: true, message: "请选择项目", trigger: "change"}
        ],
        rewardsType: [
          {required: true, message: "请选择奖惩类型", trigger: "change"}
        ],
        rewardsMoney: [
          {required: true, message: "请输入奖惩金额", trigger: "blur"}
        ],
      },
      // 项目选择
      projectOptions: [],
      // 奖惩类型
      rewardsTypeOptions: rewardsTypeOptions,
      // 审核状态
      approveStatusOptions: approveStatusOptions,
      // 选择员工弹框
      isStaffOpen: false,
      // 已选择员工
      notStaffIds: [],
      // 临时存放选择员工
      temporaryList: [],
      // 选择类型
      type: 1

    };
  },
  created() {
    this.getList();
    this.handleGetProjectList();
  },
  methods: {
    /** 查询奖惩管理列表 */
    getList() {
      this.loading = true;
      listCldRewards(this.queryParams).then(response => {
        this.CldRewardsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询项目列表
    handleGetProjectList() {
      getList({
        projectStatus: 2
      }).then(res => {
        this.projectOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rewardsId: null,
        projectId: null,
        staffId: null,
        rewardsType: null,
        rewardsRemark: null,
        rewardsMoney: null,
        createId: null,
        createName: null,
        createTime: null,
        flag: null,
        staffList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 添加员工
    handleAddStaff() {
      // 筛选已选择人员
      this.notStaffIds = this.form.staffList.map(item => item.staffId);
      this.isStaffOpen = true;
    },
    // 员工选择监听
    handleStaffSelected(e) {
      this.temporaryList = e;
    },
    // 员工确认选择
    submitSelectedStaff() {
      this.temporaryList.forEach(item => {
        this.form.staffList.push(item)
      })
      this.isStaffOpen = false;
    },
    // 移除员工
    handleDeleteStaff(e) {
      const index = this.form.staffList.indexOf(row);
      if (index > -1) {
        this.form.staffList.splice(index, 1);
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加奖惩管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.type = type;
      this.reset();
      const rewardsId = row.rewardsId || this.ids
      getCldRewards(rewardsId).then(response => {
        this.form = response.data;
        this.auditOpen = true;
        this.title = "奖惩管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rewardsId != null) {
            updateCldRewards(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.auditOpen = false;
              this.getList();
            });
          } else {
            if (this.form.staffList.length == 0) {
              return this.$message.error("请选择员工");
              return;
            }
            addCldRewards(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 审核
    handleAuditOptions(approveStatus) {
      this.form.approveStatus = approveStatus;
      if (approveStatus == 2) {
        this.$prompt('', '驳回原因', {
          center: true,
          inputType: 'textarea',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({value}) => {
          this.form.rejectReason = value;
          this.handleAudit();
        })
      } else {
        this.handleAudit();
      }
    },
    // 审核
    handleAudit() {
      auditCldRewards(this.form).then(res => {
        this.$modal.msgSuccess("操作成功");
        this.auditOpen = false;
        this.getList();
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rewardsIds = row.rewardsId || this.ids;
      this.$modal.confirm('是否确认删除奖惩管理编号为"' + rewardsIds + '"的数据项？').then(function () {
        return delCldRewards(rewardsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
